package com.fg.cloud.manager.service.factory.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.factory.FactoryNewsEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryNewsVo;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.factory.FactoryNewsMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.factory.FactoryNewsService;

/**
 * @author liushengchao
 * @Description: 新闻公告接口实现 
 * @date 2017年6月14日下午2:29:04
 */
@Service
public class FactoryNewsServiceImpl extends BaseServiceImpl implements FactoryNewsService{

	@Autowired
	private FactoryNewsMapper factoryMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return factoryMapper;
	}

	/**
	 * 厂商用户分页查询
	 */
	@Override
	public void selectforPage(PageUtils<FactoryNewsEntity> pageinfo) {
		long total = factoryMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total > 0) {
			List<FactoryNewsEntity> list = factoryMapper.findList(pageinfo);
			for (FactoryNewsEntity entity : list) {
				if (StringUtils.isNotEmpty(entity.getReciverAge())) {
					entity.setReciverAge(split(entity.getReciverAge()));
				}
				if (StringUtils.isNotEmpty(entity.getReciverArea())) {
					entity.setReciverArea(split(entity.getReciverArea()));
				}
				if (StringUtils.isNotEmpty(entity.getReciverLevel())) {
					entity.setReciverLevel(split(entity.getReciverLevel()));
				}
			}
			pageinfo.setData(list);
		} else {
			pageinfo.setData(new ArrayList<FactoryNewsEntity>());
		}
	}
	
	private String split(String text) {
		if (",".equals(text.substring(text.length()-1)))
			text = text.substring(0, text.length()-1);
		return text.replaceAll("[ ]+", "");
	}

	
	
	/**
	 * 经销商用户分页查询
	 */
	@Override
	public void selectPage(PageUtils<FactoryNewsEntity> pageinfo) {
		long total = factoryMapper.filterCounts(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total > 0) {
			pageinfo.setData(factoryMapper.findLists(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<FactoryNewsEntity>());
		}	
	}

	/**
	 * 批量删除
	 */
	@Override
	@Transactional
	public boolean deletes(Long facId, Long[] ids) {
		return factoryMapper.deletes(facId, ids)>0 ? true : false;
	}

	/**
	 * 动态查询
	 */
	@Override
	public boolean selectBycondition(FactoryNewsEntity searchNews) {
		return factoryMapper.selectBycondition(searchNews)>0 ? true : false ;
	}

	@Override
	public FactoryNewsVo find(Long id, Long facId) {
		return factoryMapper.find(id,facId);
	}

	/**
	 * 厂商新增新闻公告
	 */
	@Override
	@Transactional
	public boolean insert(FactoryNewsEntity factoryNews, FactoryUserVo facUser) {
		if (factoryNews.getReciverType()==null || factoryNews.getReciverType()==0) {
			factoryNews.setReciverType((byte) 0);
			factoryNews.setReciverAge(null);
			factoryNews.setReciverArea(null);
			factoryNews.setReciverLevel(null);
		}
		
		splitReciverInfo(factoryNews);
		
		//将当前用户的id作为creator、updater
		factoryNews.setCreator(facUser.getId());
		factoryNews.setUpdater(facUser.getId());
		int flag = factoryMapper.insertSelective(factoryNews);
		return flag > 0 ? true : false;
	}
	
	@Override
	public <T extends BaseEntity> boolean updateByPrimaryKeySelective(T t) {
		FactoryNewsEntity factoryNews = (FactoryNewsEntity) t;
		splitReciverInfo(factoryNews);
		return super.updateByPrimaryKeySelective(factoryNews);
	}
	
	
	private void splitReciverInfo(FactoryNewsEntity factoryNews) {
		if (factoryNews.getReciverType()==1 && !StringUtils.isEmpty(factoryNews.getReciverAge())){
			StringBuilder reciverAge = new StringBuilder();
			String[] strs1 = factoryNews.getReciverAge().split(",");
			for (int j = 0; j < strs1.length; j++) {
				reciverAge.append(" "+strs1[j]+",");
			}
			factoryNews.setReciverAge(reciverAge.toString());
			factoryNews.setReciverArea(null);
			factoryNews.setReciverLevel(null);
		}

		if (factoryNews.getReciverType()==2 && !StringUtils.isEmpty(factoryNews.getReciverArea())){
			StringBuilder reciverArea = new StringBuilder();
			String[] strs2 = factoryNews.getReciverArea().split(",");
			for (int j = 0; j < strs2.length; j++) {
				reciverArea.append(" "+strs2[j]+",");
			}
			factoryNews.setReciverArea(reciverArea.toString());
			factoryNews.setReciverAge(null);
			factoryNews.setReciverLevel(null);
		}

		if (factoryNews.getReciverType()==3 && !StringUtils.isEmpty(factoryNews.getReciverLevel())){
			StringBuilder reciverLevel = new StringBuilder();
			String[] strs3 = factoryNews.getReciverLevel().split(",");
			for (int j = 0; j < strs3.length; j++) {
				reciverLevel.append(" "+strs3[j]+",");
			}
			factoryNews.setReciverLevel(reciverLevel.toString());
			factoryNews.setReciverAge(null);
			factoryNews.setReciverArea(null);
		}
	}
	

}
